package com.lfd.controller;


import java.util.List;

import javax.servlet.http.HttpSession;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lfd.entity.User;
import com.lfd.service.UserServise;
import com.lfd.util.Result;
import com.lfd.util.UserUtil;



@Controller
@RequestMapping("/user")
public class UserController {
	private final Logger logger=Logger.getLogger(UserController.class);
	@Autowired
	private  UserServise userServise;
	
	
    @ResponseBody
	@RequestMapping("/loginUser")
	public Result login(@RequestParam(value = "username",required = false)String username,@RequestParam(value = "userpassword",required = false)String userpassword, HttpSession session ) {
	 User user=new User();
	 user.setUsername(username);
     user.setUserpassword(userpassword);
  		try {
			user = userServise.login(user);
			UserUtil.init(session,user);
			return new Result("0", "登录成功");
			} catch (Exception e) {
							// TODO: handle exception
				logger.error("添加失败", e);
			return new Result("1", "登录失败");
		}
		
	}
    /**
	  * 退出登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/clear")
	public Result TuiChu() {
		try {
			UserUtil.clear();
			return new Result("0", "注销成功");	
		} catch (Exception e) {
			logger.error("注销失败", e);
			return new Result("1", "注销失败");
		}
			
	}
	@ResponseBody
	@RequestMapping("/insertUser")
	public Result insert(User user) {
		try {
			userServise.insert(user);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("注册失败", e);
			return new Result("1", "注册失败");
		}
		return new Result("0", "注册成功:请登录");
		
	}
}
